package com.s69.demo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.s69.demo.dao.AppInfoDao;
import com.s69.demo.dao.AppVersionDao;
import com.s69.demo.dto.AppInfoDto;
import com.s69.demo.entity.AppInfo;
import com.s69.demo.entity.AppVersion;
import com.s69.demo.utils.SysConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

@Service
public class AppInfoService {

    @Autowired
    AppInfoDao appInfoDao;
    @Autowired
    AppVersionDao appVersionDao;

    public PageInfo<AppInfo> getPage(AppInfoDto appInfoDto, Integer pageNum) {
        AppInfo appInfo = new AppInfo();
        BeanUtils.copyProperties(appInfoDto, appInfo);
        PageHelper.startPage(pageNum, SysConstant.pageSize, "id desc");
        List<AppInfo> appInfos = appInfoDao.queryAllBy(appInfo);
        PageInfo<AppInfo> pageInfo = new PageInfo<>(appInfos);
        return pageInfo;
    }

    public AppInfo selectView(Long id) {
        return appInfoDao.queryById(id);
    }

    public void saveOrUpdate(AppInfo appInfo) {
        if (appInfo.getId() == null) {
            appInfoDao.insert(appInfo);
        } else {
            appInfoDao.update(appInfo);
        }
    }

    public void del(Long id, HttpServletRequest request) {
        AppVersion appVersion = new AppVersion();
        appVersion.setAppid(id);
        List<AppVersion> list = appVersionDao.queryAllBy(appVersion);
        for (AppVersion version : list) {
            if (StringUtils.hasText(version.getDownloadlink())) {
                String realPath = request.getServletContext().getRealPath(version.getDownloadlink());
                File file = new File(realPath);
                if (file.exists()) {
                    file.delete();
                }
            }
        }

        appVersionDao.deleteByAppid(id);

        AppInfo appInfo = appInfoDao.queryById(id);
        if (StringUtils.hasText(appInfo.getLogopicpath())) {
            String realPath = request.getServletContext().getRealPath(appInfo.getLogopicpath());
            File file = new File(realPath);
            if (file.exists()) {
                file.delete();
            }
        }
        appInfoDao.deleteById(id);

    }

    public void sale(Long id) {
        AppInfo appInfo = appInfoDao.queryById(id);
        if (appInfo.getStatus() == SysConstant.AppStatus.on) {
            appInfo.setStatus(SysConstant.AppStatus.off);
        } else if (appInfo.getStatus() == SysConstant.AppStatus.off) {
            appInfo.setStatus(SysConstant.AppStatus.on);
        } else if (appInfo.getStatus() == SysConstant.AppStatus.yes) {
            appInfo.setStatus(SysConstant.AppStatus.on);
        }
    }

    public void review(Long appid, Integer type) {
        AppInfo appInfo = new AppInfo();
        appInfo.setId(appid);
        if (type == 1) {
            appInfo.setStatus(2L);
        } else if (type == 2) {
            appInfo.setStatus(3L);
        }
        appInfoDao.update(appInfo);
    }

    public Boolean validate(AppInfo appInfo) {
        if (appInfo.getId() != null) {
            AppInfo app = appInfoDao.queryById(appInfo.getId());
            if (app.getApkname().equals(appInfo.getApkname())) {
                return true;
            }
        }

        Integer count = appInfoDao.validateApkName(appInfo.getApkname());
        if (count > 0) {
            return false;
        }
        return true;
    }
}
